<template>
  <div class="login-container">
    <van-nav-bar left-text="返回" @click-left="back" title="登录"
      class="page-nav-bar" />
    <van-form @submit="onSubmit">
      <van-field v-model="user.mobile" center name="用户名" maxlength="11"
        :rules="rule.mobile" type="numbr" placeholder="请输入手机号">
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="user.code" center name="验证码" maxlength="6"
        :rules="rule.Code" type="number" placeholder="请填写验证码">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-button v-if="flag" class="send-sms-btn" native-type="button"
            round size="small" type="default" @click="verCode">
            发送验证码</van-button>
          <van-count-down v-else @finish='isFlag' :time="5 * 1000"
            format="ss s" />
        </template>

      </van-field>
      <div style="margin: 16px">
        <van-button class="btn-denglu" primary block type="info"
          native-type="submit">登陆
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { userCode as userCodeAPI, verCode as verCodeAPI } from '@/api/user.js'
export default {
  data () {
    return {
      time: 30 * 60 * 60 * 1000,
      user: {
        mobile: '',
        code: '',
        timer: null

      },
      flag: true,
      rule: {
        mobile: [
          {
            required: true,
            message: '手机号不能为空'
          },
          {
            pattern: /^1[0-9]{10}$/,
            message: '手机号格式不正确'
          }
        ],
        Code: [
          {
            required: true,
            message: '验证码不能为空'
          },
          {
            pattern: /^\d{6}$/,
            message: '请输入6位的验证码'
          }
        ]
      }
    }
  },
  methods: {
    // onSubmit (values) {
    //   console.log('submit', values)
    // }
    async onSubmit () {
      this.$toast.loading({
        message: '登陆中...',
        forbidClick: true,
        duration: 0

      })
      const that = this
      this.timer = setTimeout(() => {
        return that.$toast.fail('登陆超时,请检查网络')
      }, 5000)
      try {
        const res = await userCodeAPI(this.user)
        clearTimeout(this.timer)

        this.$toast.success('登陆成功')
        this.$router.push({
          path: '/layout/my'
        })
        this.$store.commit('setUser', res.data.data.token)
        document.cookie = { a: res.data.data.token }
      } catch (err) {
        console.log('错误')
        if (err.response.status === 400) {
          clearTimeout(this.timer)
          this.$toast.fail('手机号或验证码错误')
          console.log('手机号或验证码错误', err)
        } else {
          // return throw '错误'
          clearTimeout(this.timer)
          this.$toast.fail('登陆失败，请稍后重试')
          console.log('登陆失败，请稍后重试', err)
        }
      }
    },
    async verCode () {
      this.flag = false
      console.log(this.user.mobile)
      const res = await verCodeAPI(this.user.mobile)
      console.log(res)
    },
    isFlag () {
      this.flag = true
    },
    back () {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
    color: red;
  }
}
.login-container {
  .btn-denglu {
    background-color: #24abf2;
  }
}
</style>
